package site.wetsion.campusgrade.codeimg;

import java.awt.image.BufferedImage;
import java.io.File;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
/**
 * 利用tesseract-oc识别验证码图片
 * 
 * 前提：服务器所在机器安装了tesseract
 * @author Wetsion
 *
 */
public class Tess4jDiscern {
	
	public static String getCode(BufferedImage file){
		ITesseract tesseract = new Tesseract();
		tesseract.setDatapath("D:\\Program Files (x86)\\Tesseract-OCR\\tessdata");  
		String result = null;
		try {
			result = tesseract.doOCR(file).trim().replace(" ", "");
			System.out.println(result);
		} catch (TesseractException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		File file = new File("D:\\code.jpg");
		ITesseract tesseract = new Tesseract();
		tesseract.setDatapath("D:\\Program Files (x86)\\Tesseract-OCR\\tessdata");  
		try {
			String result = tesseract.doOCR(file);
			System.out.println(result);
		} catch (TesseractException e) {
			e.printStackTrace();
		}
	}

}
